<template>
    <div class="ct-bodySet">
        <el-container>
            <el-header class="bgclr-dk-sp0 mb20 headerCoster">
                <el-row>
                    <el-col :span="20">
                        <el-breadcrumb separator-class="el-icon-arrow-right">
                            <el-breadcrumb-item :to="{ path: '/mainPage/welcome' }">首页</el-breadcrumb-item>
                            <el-breadcrumb-item>周报管理</el-breadcrumb-item>
                            <el-breadcrumb-item><span class="main-Clr-ft">本周工作概述</span></el-breadcrumb-item>
                        </el-breadcrumb>
                    </el-col>

                    <el-col :span="4" class="textAlignRight buttonLineHeight" style="line-height: 60px">

                        <el-button type="text" class="main-Clr-ft" icon="el-icon-back" @click="backOffBtn"   size="small">返回</el-button>
                    </el-col>
                </el-row>
            </el-header>
            <div class="pl20 pr20">
                <el-button v-show="exportFlag" style="float: right;margin-right: 5%;font-size: 20px" type="text" @click="exportWeek()">导出</el-button>
                <el-button  style="float: right;margin-right: 5%;font-size: 20px" type="text" @click="editSummary()">编辑</el-button>
                <el-button  style="float: right;margin-right: 5%;font-size: 20px" type="text" @click="submitProduct()">提交</el-button>
                <el-card class="mb20 cardWidth" v-model="productSummaryFlag" v-show="productSummaryFlag">
                    <div slot="header">
                        <span style="color: #2FB2B8">一、产品研发</span>
                        <span style="color: #2FB2B8" :data="weekSummary.productCount"  v-model="weekSummary.productCount">{{weekSummary.productCount}}</span>
                        <span style="color: red">（建议填写工作概述时，不要填写编号，即使填写也会被处理掉。）</span>
                    </div>
                        <el-col  :span="22"
                            :data="weekSummary.productWeek"
                             style="margin-left: 3%"
                             v-for=" item in weekSummary.productWeek" class="colbuttom">
                        <div class="divstyle">{{item.num+item.taskName}}---{{item.userName}}</div>
                        <br>
                        <span v-if="item.editSet">
                                <el-input   type="textarea" v-model="item.workSummary" :autosize="{ minRows: 2}"></el-input>
                        </span>
                        <span v-else>
                                <div  class="divstyle2"  >{{item.workSummary}}</div>
                        </span>
                    </el-col>
                </el-card>
                <el-card class="mb20 cardWidth" v-model="projectSummaryFlag" v-show="projectSummaryFlag">
                    <div slot="header">
                        <span style="color: #2FB2B8">二、项目支持</span>
                        <span style="color: #2FB2B8" :data="weekSummary.projectCount"  v-model="weekSummary.projectCount">{{weekSummary.projectCount}}</span>
                    </div>
                    <el-col  :span="22"
                             :data="weekSummary.projectWeek"
                             style="margin-left: 3%"
                             v-for=" item in weekSummary.projectWeek" class="colbuttom">
                        <div class="divstyle">{{item.num + item.taskName}}</div>
                        <br>
                        <span v-if="item.editSet" >
                            <span v-for="item1 in item.workSummaryList">
                                <el-input v-model="item1.workContent" type="textarea" :autosize="{ minRows: 2}"></el-input>
                            </span>
                        </span>
                        <span v-else >
                            <span v-for="item1 in item.workSummaryList">
                                <div  class="divstyle2">{{item1.workContent}}</div>
                            </span>
                        </span>

                    </el-col>
                </el-card>
            </div>
            <div class="bottomLayoutBus"></div>
        </el-container>
<!--        <span>{{weekSummary.productWeek}}</span>-->
    </div>
</template>

<script>
    import urlConfig from '../../../../config/url.json';
    import Vue from 'vue';
    import { Message } from 'element-ui';

    Vue.component(Message.name, Message);

    let weekSummaryUrl = urlConfig.weekSummaryUrl;
    let submitWeekSummaryUrl = urlConfig.submitWeekSummaryUrl;
    let exportWeekUrl = urlConfig.exportWeekUrl;
    let getMonthWeekUrl = urlConfig.getMonthWeekUrl;

    export default {
        data() {
            return {
                pickerOptions: {
                    firstDayOfWeek:1,
                },
                weekId:'',
                weekSummary: {
                    productCount:"",
                    productWeek:[],
                    projectCount:"",
                    projectWeek:[],
                },
                userIdList:[],
                roleId:"",
                productSummaryFlag:true,
                projectSummaryFlag:true,
                exportFlag:false,
                week:"",
                startDate:"",
                endDate:"",
                year:"",
                fileName:"",
                noSubmitOfGroup:[],
                noSubmitProductOfUser:[],
                mainProductList:[]
            }
        },
        methods: {
            getFileName() {
                let that = this;
                const loading = that.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                });
                that.$http({
                    method: 'post',
                    url: getMonthWeekUrl,
                    baseURL: '/api',
                    data: {
                        "weekId": that.weekId,
                        "userIdList": that.userIdList,
                        "groupIdList": JSON.parse(that.STORAGE.getItem("groupIdList")),
                        "department": that.STORAGE.getItem("department"),
                        "week": that.week,
                        "startDate": that.startDate,
                        "endDate": that.endDate,
                        "year":that.year,
                    },
                }).then(function (response) {
                    debugger;
                    if (response.data.code == 200){
                         that.fileName = response.data.data.str;
                    }else{

                    }

                }).catch(function (err) {
                    console.log(err);
                    Message.error('获取文件名称失败');
                    loading.close();
                });
            },
            exportWeek() {
                let that = this;
                const loading = that.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                });
                that.$http({
                    method: 'post',
                    url: exportWeekUrl,
                    baseURL: '/api',
                    data: {
                        "weekId": that.weekId,
                        "userIdList": that.userIdList,
                        "groupIdList": JSON.parse(that.STORAGE.getItem("groupIdList")),
                        "department": that.STORAGE.getItem("department"),
                        "week": that.week,
                        "startDate": that.startDate,
                        "endDate": that.endDate,
                        "year":that.year,
                        "userId":(JSON.parse(that.STORAGE.getItem("loginData"))).id,
                    },
                    responseType: 'blob'
                }).then(function (response) {
                    debugger;
                    if(that.fileName.length==0){
                        that.fileName = "云医疗事业部-" + that.STORAGE.getItem("department").replace("开发部", "") +
                            "产品线-研发周报(" + that.week.replace(/\s/g, "") + ").xlsx";//文件名称
                    }
                    const link = document.createElement('a');
                    let blob = new Blob([response.data], {type: 'application/vnd.ms-excel'});
                    link.style.display = "none";
                    link.href = URL.createObjectURL(blob);
                    link.setAttribute('download', that.fileName);
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                    loading.close();
                }).catch(function (err) {
                    console.log(err);
                    Message.error('导出周报失败');
                    loading.close();
                });

            },
            //返回
            backOffBtn: function () {
                this.$router.go(-1);
            },
            submitWeekSummary() {
                let that = this;
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                });
                that.$http({
                    method: 'post',
                    url: submitWeekSummaryUrl,
                    baseURL: '/api',
                    data: {
                        'weekId': that.weekId,
                        "productWeek": that.weekSummary.productWeek,
                        "projectWeek": that.weekSummary.projectWeek,
                        "groupId": JSON.parse(that.STORAGE.getItem("groupIdList"))[0]["id"],
                        "userId": JSON.parse(that.STORAGE.getItem("loginData"))['id']
                    }
                }).then(function (response) {
                    if (response.data.code == 200) {
                        that.queryWeekSummary(false);
                        Message.success("提交周报概述成功")
                    } else {
                        Message.error('提交周报概述失败');
                    }
                    loading.close();
                }).catch(function (err) {
                    console.log(err);
                    Message.error('提交周报概述失败');
                    loading.close();
                });

            },
            submitProduct() {
                let that = this;
                var count = 0;
                if(that.productSummaryFlag != false){
                    for (var i = 0; i < that.weekSummary.productWeek.length; i++) {
                        if (that.weekSummary.productWeek[i].editSet == true) {
                            ++count;
                        }
                    }
                }else{
                    that.weekSummary.productWeek=[];
                }
                if(that.projectSummaryFlag != false){
                    for (var i = 0; i < that.weekSummary.projectWeek.length; i++) {
                        if (that.weekSummary.projectWeek[i].editSet == true) {
                            ++count;
                        }
                    }
                }else{
                    that.weekSummary.projectWeek=[];
                }

                if (count == 0) {
                    Message.warning("没有要提交的概述！")
                } else {
                    that.submitWeekSummary()
                }
            },
            editSummary() {
                let that = this;
                if(that.productSummaryFlag != false){
                    for (var i = 0; i < that.weekSummary.productWeek.length; i++) {
                        that.weekSummary.productWeek[i].editSet = true;

                    }
                }
                if(that.projectSummaryFlag != false){
                    for (var i = 0; i < that.weekSummary.projectWeek.length; i++) {
                        that.weekSummary.projectWeek[i].editSet = true;
                    }
                }

            },
            queryWeekSummary(editSet) {
                debugger;
                let that = this;
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                });
                that.$http({
                    method: 'post',
                    url: weekSummaryUrl,
                    baseURL: '/api',
                    data: {
                        'weekId': that.weekId,
                        "productWeek": that.weekSummary.productWeek,
                        "projectWeek": that.weekSummary.projectWeek,
                        "userIdList": that.userIdList,
                        "groupIdList": JSON.parse(that.STORAGE.getItem("groupIdList")),
                        "editSet": editSet,
                        "roleId": that.roleId,
                        "mainProductList":that.mainProductList
                    }
                }).then(function (response) {
                    debugger;
                    if (response.data.code == 200) {
                        if (response.data.data.productWeek.length == 0) {
                            that.productSummaryFlag = false;
                        } else {
                            that.weekSummary.productWeek = response.data.data.productWeek;
                        }
                        if (response.data.data.projectWeek.length == 0) {
                            that.projectSummaryFlag = false;
                        } else {
                            that.weekSummary.projectWeek = response.data.data.projectWeek;

                        }
                        that.calculateSubmitProductSummary();
                    } else {
                        Message.error('获取数据失败');
                    }
                    loading.close();
                }).catch(function (err) {
                    console.log(err);
                    Message.error('获取数据失败');
                    loading.close();
                });
            },
            //页面间传参
            getParams: function () {
                let that = this;
                //取到路由带过来的参数
                let routerParams = this.$route.query;
                that.weekId = routerParams.weekId;
                that.weekSummary.productWeek = routerParams.productWeek;
                that.weekSummary.projectWeek = routerParams.projectWeek;
                // that.weekSummary.productCount = "（" + routerParams.productCount + "人）";
                // that.weekSummary.projectCount = "（" + routerParams.projectCount + "人）";
                that.userIdList = routerParams.userIdList;
                that.roleId = routerParams.roleId;
                that.week = routerParams.week;
                that.startDate = routerParams.startDate;
                that.endDate = routerParams.endDate;
                that.year = routerParams.year;
                that.mainProductList = routerParams.mainProductList;

            },
            roleEstimate() {
                debugger
                let that = this;
                if (that.roleId == '10002') {
                    that.queryWeekSummary(true)
                }
                if (that.roleId == '10004' || that.roleId == '10001') {
                    that.queryWeekSummary(false);
                    that.exportFlag = true;
                }
                if (that.roleId == '10003') {
                    that.queryWeekSummary(true)
                }
            },

            calculateSubmitProductSummary(){
                debugger
                let that = this;
                var productList = that.weekSummary.productWeek;
                var arr = new Array();
                if(that.productSummaryFlag!=false){
                    for (var i = 0; i < productList.length; i++) {//所有研发研发任务循环
                        var workSummary = productList[i].workSummary;
                        if(workSummary.length==0){
                            if(arr.length==0){
                                var map = {
                                    "productName": productList[i].taskName,
                                    "userId": productList[i].userId,
                                    "userName": productList[i].userName,
                                };
                                arr.push(map);
                            }else{
                                for(var j=0;j<arr.length;j++){
                                    if(productList[i].userId==arr[j].userId){
                                        arr[j].taskName +="、" + productList[i].taskName;
                                    }else{
                                        if(arr.length-1==j){
                                            var map = {
                                                "productName": productList[i].taskName,
                                                "userId": productList[i].userId,
                                                "userName": productList[i].userName,
                                            };
                                            arr.push(map);
                                        }
                                        continue;
                                    }
                                }
                            }
                        }else{
                            continue
                        }
                    }
                }
                debugger;
                that.noSubmitProductOfUser = arr;
                that.calculateSubmitGroup();
                },

            calculateSubmitGroup() {
                let that = this;
                var productList = that.weekSummary.productWeek;
                var arr = new Array();//存放未填写组概述的小组
                var projectList = that.weekSummary.projectWeek;
                if(that.projectSummaryFlag!=false){
                    for (var i = 0; i < projectList.length; i++) {
                        var workSummaryList = projectList[i].workSummaryList;
                        for (var j = 0; j < workSummaryList.length; j++) {
                            if (workSummaryList[j].workContent.length == 0) {
                                if (arr.length == 0) {
                                    var map = {
                                        "projectName": projectList[i].taskName,
                                        "groupId": workSummaryList[j].groupId,
                                        "groupName": workSummaryList[j].groupName,
                                    };
                                    arr.push(map);
                                } else {
                                    var count=1;
                                    for(var k=0;k<arr.length;k++){
                                        if(arr[k].groupId==workSummaryList[j].groupId){
                                            if(arr[k].projectName.length==0){
                                                arr[k].projectName +=projectList[i].taskName;
                                            }else{
                                                arr[k].projectName +="、" + projectList[i].taskName;
                                            }
                                            break;
                                        }else{
                                            if(count == arr.length){
                                                var map = {
                                                    "projectName": projectList[i].taskName,
                                                    "groupId": workSummaryList[j].groupId,
                                                    "groupName": workSummaryList[j].groupName,
                                                };
                                                arr.push(map);
                                                break;
                                            }else{
                                                ++count;
                                            }
                                        }
                                    }
                                }
                            } else {
                                continue;
                            }
                        }
                    }
                }
                that.noSubmitOfGroup = arr;
                if(that.noSubmitOfGroup.length!=0 || that.noSubmitProductOfUser.length!=0) {

                    that.noSubmitPersonMessage();
                }
            },
            queryGroupName(groupId) {
                let that = this;
                var groupInfoList = JSON.parse(that.STORAGE.getItem("groupIdList"));
                for (var i = 0; i < groupInfoList.length; i++) {
                    if (groupInfoList[i].id == groupId) {
                        return groupInfoList[i].groupName;
                    } else {
                        continue;
                    }
                }
            },
            noSubmitPersonMessage() {
                let that = this;
                var message = "";
                debugger;
                var k = 1
                if(that.noSubmitProductOfUser.length!=0){
                    for(var j = 0; j < that.noSubmitProductOfUser.length; j++){
                        if(that.noSubmitProductOfUser[j].userId==null){
                            message += '<div><span class="messageStyle1">' +k+'、'+ "XXX老师"+'</span>： </div>';
                        }else{
                            message += '<div><span class="messageStyle1">' +k+'、'+ that.noSubmitProductOfUser[j].userName + "老师"+'</span>： </div>';
                        }
                        if(that.noSubmitProductOfUser[j].productName.length==0){
                            message +='<div>研发任务：全部提交</div>'
                        }else{
                            message +='<div>研发任务：<span class="messageStyle">'+that.noSubmitProductOfUser[j].productName+'' +
                                '</span><span>未提交概述</span></div>'
                        }
                        ++k;
                    }
                }
                for (var i = 0; i < that.noSubmitOfGroup.length; i++) {

                    message += '<div><span class="messageStyle1">' +k+'、'+ that.noSubmitOfGroup[i].groupName + '</span>： </div>';
                    // if(that.noSubmitOfGroup[i].productName.length==0){
                    //     message +='<div>研发任务：全部提交</div>'
                    //
                    // }else{
                    //     message +='<div>研发任务：<span class="messageStyle">'+that.noSubmitOfGroup[i].productName+'' +
                    //         '</span><span>未提交概述</span></div>'
                    // }
                    if(that.noSubmitOfGroup[i].projectName.length==0){
                        message +='<div>项目支持：全部提交</div>'
                    }else{
                        message +='<div>项目支持：<span class="messageStyle">'+that.noSubmitOfGroup[i].projectName+'' +
                            '</span><span>未提交概述</span></div>'
                    }
                    ++k;
                }

                that.$notify({
                    title: '提醒',
                    message: message,
                    dangerouslyUseHTMLString: true,
                    offset: 50,
                    // duration: 0
                });
            },
        },
        mounted() {
            let that=this;
            that.groupIdList = JSON.parse(that.STORAGE.getItem("groupIdList"));
            Vue.prototype.quitLogin=false;
            that.getParams();
            that.roleEstimate();
            if(that.exportFlag==true){
                that.getFileName();
            }
        },

    }
</script>

<style scoped>
    .headerCoster{
        height: 59px;
        border-top: 1px solid #eee;
        border-bottom: 1px solid #eee;
    }

    .deleteButtonColor{
        color: red;
    }

    .letBorder{
        border-left: #6aabe6 solid 1px;
    }
    .cardWidth{
        width: 100%;
    }
    .el-select{
        width: 100%;
    }
    .colbuttom{
        margin-bottom: 2%;
    }
    .divstyle2{
        margin-left: 1%;
        white-space:pre-wrap;line-height:1.5;
    }
    .divstyle{
        font-weight: bold;color: #2FB2B8
    }
</style>